package br.com.lh.urabuilder.command
{
	import br.com.lh.urabuilder.business.ViewManager;
	import br.com.lh.urabuilder.command.base.UraBaseCommand;
	import br.com.lh.urabuilder.event.AddActionEvent;
	import br.com.lh.urabuilder.event.SetInitialActionEvent;
	import br.com.lh.urabuilder.view.ActionComponent;
	
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	public class AddActionCommand extends UraBaseCommand
	{
		private var currentElement:ActionComponent;
		
		public function AddActionCommand(){
			text = "teste";
		}
		
		public override function redo( event: CairngormEvent =null): void
		{
			var addActionEvent : AddActionEvent =
				AddActionEvent( event );
			if (addActionEvent != null){
				this.currentElement = addActionEvent.action;
			}
			
			uraModelLocator.viewManager.insertComponent(currentElement);
			uraModelLocator.xmlManager.insertElement(currentElement.uraAction);
			updateCombo();
			currentElement.setFocus();
		}
		
		public override function undo():void{
			var viewManager:ViewManager = uraModelLocator.viewManager;
			viewManager.removeComponent(currentElement);
			uraModelLocator.xmlManager.removeElement(currentElement.uraAction);
		}
		
		private function updateCombo():void{
			uraModelLocator.selectableComponents.updateSelectableList();
			if (uraModelLocator.uraPropertiesForm !=null){
				uraModelLocator.uraPropertiesForm.initialAction.executeBindings();
			}
			if (uraModelLocator.viewManager.getComponents().length ==1){
				CairngormEventDispatcher.getInstance().dispatchEvent(new SetInitialActionEvent(currentElement.id as int));
			}
		}
		
		
		
		
		

	}
}